//排班类型为设备
var schedu_type='1';
var addDate='';
$(document).ready(function(){
	commonSet();
	//加载查询条件
	initQueryCondition().then(function(){
		//日历初始化
		initCalendar();
	});
	//事件绑定
	eventBind();
});

function commonSet(){
	initLoader();
}

//事件绑定
function eventBind(){
	//排班信息查询
	$("#queryBtn").click(function(){
		var view = $('#calendar').fullCalendar('getView');
		queryData(view.start.format("YYYY-MM-DD"),view.end.format("YYYY-MM-DD"));
	});
}

//加载查询条件
function initQueryCondition(){
	var def = $.Deferred();
	//科室设备下拉框
	$.getApiClient('/scheduling/depts').get().done(function(data){
		if(!data.payload){
			data.payload=[];
		}
	    multipleSelectData("schedu_did",data.payload, data.payload[0].id || null, {valueAttr:'id', displayAttr:'department_name'});
		$('#schedu_did').multipleSelect({width:'100%', single: true});
		
		$('#schedu_did').change(function(){
			var deptId=$(this).children('option:selected').val();
			loadDeviceSelect(deptId);
		}).change();
	});
	
	function loadDeviceSelect(deptId){
		if(deptId){
			$.getApiClient('/scheduling/devices').setQuery({department_id:deptId}).get().done(function(data){
				if(!data.payload){
					data.payload=[];
				}
				if(data.payload.length > 0){
				    multipleSelectData("device_id",data.payload, data.payload[0].id || null, {valueAttr:'id', displayAttr:'device_name'});
					$('#device_id').multipleSelect({width:'100%', single: true});
				}else{
					multipleSelectData("device_id",[{"id": "", "device_name": "请选择"}], null, {valueAttr:'id', displayAttr:'device_name'},"请选择");
					$('#device_id').multipleSelect({width:'100%', single: true});
				}
				def.resolve();
			});
		}else{
			multipleSelectData("device_id",[{"id": "", "device_name": "请选择"}], null, {valueAttr:'id', displayAttr:'device_name'},"请选择");
			$('#device_id').multipleSelect({width:'100%', single: true});
			def.resolve();
		}
	}
	return def.promise();
}

//定义日历UI设置对象
var calendarUI={
	header: {
		left: 'prev,next today',
		center: 'title',
		right: ''
	},
	//默认周视图
	contentHeight: 570,
	defaultView:'agendaWeek',
	firstDay: 1,
	theme:false,
	allDaySlot:false,
	allDayText:'全天',
	today: ["今天"],   
	buttonText: {
      today: '今天',    
      month: '月',    
      week: '周',    
      day: '日',    
      prev: '上一周',    
      next: '下一周'    
  },    
	monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
	monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
	dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
	dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
	editable: false,
	timeFormat:'HH:mm',
	axisFormat:'HH:mm',
	slotMinutes:30,
	minTime:'8:00',
	maxTime:'21:00',
	eventLimit: false,
	titleFormat :'YYYY年 MMM D 日',
	events: []
};

//排班管理事件列表
var calendarEvent={
	dayClick: function(date, allDay, jsEvent, view) {
		addDate=date.format("YYYY-MM-DD HH:mm");
		showAddFormDialog();
	},
	eventClick: function(calEvent, jsEvent, view) {
		showEditFormDialog(calEvent.id);
	},
	viewRender: function (view) {       
		queryData(view.start.format("YYYY-MM-DD"),view.end.format("YYYY-MM-DD"));
   }
};

/**
 * 日历初始化
 */
function initCalendar(){
	$('#calendar').fullCalendar($.extend(calendarUI,calendarEvent));
}

/**
 * 排班列表查询
 */
function queryData(startDate,endDate){
	var view = $('#calendar').fullCalendar('getView');
    var extendParam = {
    	schedu_type : schedu_type,
    	start_date : view.start.format("YYYY-MM-DD"),
    	end_date : view.end.format("YYYY-MM-DD")
    }
	
	$.getApiClient('/scheduling/manage/list').setQuery($.extend(extendParam,$("#queryForm").collect())).get().done(function(data){
		renderCalendar(data);
	});
};

/**
 * 根据排班数据渲染日历
 */
function renderCalendar(scheduleData){
	$('#calendar').fullCalendar('removeEvents');
	$.each(scheduleData, function (index, item) {
		 var event = new Object(); 
		 event.id=item.id;
		 event.title=item.device_name+" ("+item.schedu_num+") "; 
		 event.start=$.dateFormat(item.schedu_date, "yyyy-MM-dd")+'T'+item.schedu_start;
		 event.end=$.dateFormat(item.schedu_date, "yyyy-MM-dd")+'T'+item.schedu_end;
		 event.allDay=false;
		 event.textColor="white";
		 event.color='green';
		 event.backgroundColor='green';
		 event.className ='button_pointer';
  		$('#calendar').fullCalendar('renderEvent', event);
  	 });
}

//新增排班弹出
function showAddFormDialog(){
	customRemoteUrlDialog('新增排班','/page/business/scheduling/device/new/form?type=add',null);
}

//修改排班弹出
function showEditFormDialog(scheduId){
	customRemoteUrlDialog('修改排班','/page/business/scheduling/device/new/form?type=edit&scheduId='+scheduId,null);
}